package com.dongdongshop.service.impl;


import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.model.TbAddress;
import com.dongdongshop.model.TbAddressExample;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.vo.AddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private TbAddressMapper am;


    //根据用户ID获取收获地址

    @Override
    public List<AddressVo> getAddress(String userId) {
        TbAddressExample example = new TbAddressExample();
        TbAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<TbAddress> addres = am.selectByExample(example);
        List<AddressVo> voList = addres.stream().map(a -> {
            AddressVo vo = new AddressVo();
            BeanUtils.copyProperties(a, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    //增加地址

    @Override
    public void addAddress(String username, AddressVo vo) {
        TbAddress address = new TbAddress();
        BeanUtils.copyProperties(vo, address);
        address.setUserId(username);
        am.insert(address);
    }

    //根据用户Id获取收获地址

    @Override
    public AddressVo getAddressById(Long arrderssId) {
        TbAddress address = am.selectByPrimaryKey(arrderssId);
        AddressVo vo = new AddressVo();
        BeanUtils.copyProperties(address, vo);
        return vo;
    }
}
